﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Engine.CustomControls
{
    public partial class SpeedCamerasGridView : System.Windows.Forms.DataGridView
    {
        private ContextMenu ctxMenu = new ContextMenu();


        public WebMap map;

        public SpeedCamerasGridView()
        {
            InitializeComponent();
            initView();
        }


        private void initView()
        {

            // init context menu
            MenuItem mnuShow = new MenuItem();
            mnuShow.Text = "Show";
            mnuShow.Click+=new EventHandler(mnuShow_Click);
            
            ctxMenu.MenuItems.Add(mnuShow);            


            this.AutoGenerateColumns = false;
            this.DoubleBuffered = true;
            this.RowHeadersVisible = false;
            this.MultiSelect = false;
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.AllowUserToResizeRows = false;
            


            DataGridViewTextBoxColumn cRowIndex = new DataGridViewTextBoxColumn();
            cRowIndex.Width = 30;
            
            DataGridViewCheckBoxColumn cShow = new DataGridViewCheckBoxColumn(true);
            cShow.HeaderText = "Show";
            cShow.Width = 25;
            //cShow.DataPropertyName = "Location";

            DataGridViewTextBoxColumn cName = new DataGridViewTextBoxColumn();
            cName.HeaderText = "Name";
            cName.DataPropertyName = "Location";

            DataGridViewTextBoxColumn cType = new DataGridViewTextBoxColumn();
            cType.HeaderText = "Type";
            cType.DataPropertyName = "Type";
            cType.SortMode = DataGridViewColumnSortMode.Automatic;

            DataGridViewTextBoxColumn cSpeed = new DataGridViewTextBoxColumn();
            cSpeed.HeaderText = "Speed";
            cSpeed.DataPropertyName = "Speed";
            cSpeed.Width = 40;

            DataGridViewTextBoxColumn cDir = new DataGridViewTextBoxColumn();
            cDir.HeaderText = "Direction";
            cDir.DataPropertyName = "Direction";
            cDir.Width = 40;

            DataGridViewTextBoxColumn cAngle = new DataGridViewTextBoxColumn();
            cAngle.HeaderText = "Angle";
            cAngle.DataPropertyName = "Angle";
            cAngle.Width = 40;

            this.Columns.Add(cRowIndex);
            this.Columns.Add(cShow);
            this.Columns.Add(cName);
            this.Columns.Add(cType);
            this.Columns.Add(cSpeed);
            this.Columns.Add(cDir);
            this.Columns.Add(cAngle);


            this.CellFormatting+=new DataGridViewCellFormattingEventHandler(SpeedCamerasGridView_CellFormatting); 
            this.CellMouseUp+=new DataGridViewCellMouseEventHandler(SpeedCamerasGridView_CellMouseUp);
        }


        #region Context Menu Events

        private void mnuShow_Click(object sender, EventArgs e)
        {
            Engine.POI.Location l = this.SelectedRows[0].Cells[2].Value as Engine.POI.Location;
            map.Commands.ZoomOnPoint(l.X,l.Y, 15);
        }

        #endregion

        #region DataGrid Events

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SpeedCamerasGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.Rows[e.RowIndex].Selected = true;
                int x = e.Location.X;
                for (int i = 0; i < e.ColumnIndex - 1; i++)
                {
                    x += this.Columns[i].Width;
                }                                 
                int y = this.RowTemplate.Height * e.RowIndex + e.Location.Y;
                ctxMenu.Show(this, new System.Drawing.Point(x,y));
            }
        }


        private void SpeedCamerasGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 0)
            {
                e.Value = e.RowIndex;
            }
        }

        #endregion

        public void Load(POI.PoiList list)
        {
            this.DataSource = list.ConvertTo<POI.CameraPoi>();
            map.Commands.AddPOIs(list);
        }
    }
}
